//
//  TabBarController.m
//  MooMusic
//
//  Created by wjy on 15/10/27.
//  Copyright © 2015年 wjy. All rights reserved.
//

#import "TabBarController.h"

@interface TabBarController ()<SendIsCutMusic>
@property (nonatomic, assign) BOOL isplaybool;
@end

@implementation TabBarController
+ (TabBarController *)shareTabBar
{
    static TabBarController *tabBar;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!tabBar) {
            tabBar = [[TabBarController alloc] init];
        }
    });
    return tabBar;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    
    self.tabBarView = [[TabBarView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 49, SCREENWIDTH, 49)];
    _tabBarView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.tabBarView];
    
    [self.tabBarView.songStopORStart addTarget:self action:@selector(playOrPause) forControlEvents:UIControlEventTouchUpInside];
//    [self.tabBarView.songStopORStart addTarget:self action:@selector(playOrPause:) forControlEvents:UIControlEventTouchUpInside];

    
    [self.tabBarView.songsList addTarget:self action:@selector(modalToPlay) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.tabBarView.songImage addTarget:self action:@selector(modalToPlay) forControlEvents:(UIControlEventTouchUpInside)];
}
//-(void)playOrPause:(UIButton *)button
//{
//
//    if (button.selected == NO) {
//        [self.tabBarView.songStopORStart setImage:[UIImage imageNamed:@"playMusic"] forState:(UIControlStateNormal)];
//        self.angle = 0;
//
//    }else {
//        [self.tabBarView.songStopORStart setImage:[UIImage imageNamed:@"pause"] forState:(UIControlStateNormal)];
//        self.stopAngle = 10;
//        [self startAnimation];
//    }
//    button.selected =! button.selected;
//}
#pragma mark - button点击方法
- (void)playOrPause
{
//        self.stopAngle = 0;
    if (self.player.state == STKAudioPlayerStatePlaying) {
        //定时器停止
        //        [_timer invalidate];
        
        [self.tabBarView.songStopORStart setImage:[UIImage imageNamed:@"playMusic"] forState:(UIControlStateNormal)];
        [self.player pause];
        self.stopAngle = 0;
        
        [self startAnimation];
        
    } else {
        //        _timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(startAnimation) userInfo:nil repeats:YES];
        
        [self.tabBarView.songStopORStart setImage:[UIImage imageNamed:@"pause"] forState:(UIControlStateNormal)];
        [self.player resume];
        self.stopAngle = 5;
        [self startAnimation];
    }
}


#pragma mark - 动画
- (void)startAnimation
{
    CGAffineTransform endAngle = CGAffineTransformMakeRotation(self.angle * (M_PI / 180.0f));
    
    [UIView animateWithDuration:0.358 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.tabBarView.songImage.transform = endAngle;
    } completion:^(BOOL finished) {
        if (YES) {// <- self.tabBarView.songStopORStart.selected
            self.angle += self.stopAngle;
            
            [self startAnimation];
        }
//                NSLog(@"%.f", self.angle);
    }];
    
    
    //    self.tabBarView.songImage.transform = CGAffineTransformRotate(self.tabBarView.songImage.transform, 0.1);
    
}







#pragma mark - 模态
- (void)modalToPlay
{
    WJYPalyMusicViewController *play = [[WJYPalyMusicViewController alloc] init];
    //    play.arr = [NSMutableArray arrayWithArray:self.array];
    play.stopAngle = self.stopAngle;
    //    play.index = self.index;
    play.imgName = self.imgName;
    
    
    play.delegate = self;
    
    play.isCutMusic = self.cutMusic;
    
    if (self.player.state == STKAudioPlayerStatePlaying) {
        play.buttonImg = @"pause";
        play.isplaybool = YES;
    } else {
        play.buttonImg = @"playMusic";
        play.isplaybool = NO;
    }
    
    play.songTitle = self.tabBarView.songName.text;
    [self presentViewController:play animated:YES completion:^{
        
    }];
}


// 播放方法
- (void)play:(NSString *)urlStr
{
    [self stop];
    self.player = [[STKAudioPlayer alloc] init];
    [self.player play:urlStr];
    
    
//        [self playOrPause];
    
    
}


// 停止播放
- (void)stop
{
    if (self.player) {
        [self.player stop];
    }
}


#pragma mark - 协议方法
- (void)sendIsCutMusic:(NSInteger)isCutMusic
{
    self.cutMusic = isCutMusic;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
